package com.zxcz.controller;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zxcz.domain.PageResult;
import com.zxcz.domain.Result;
import com.zxcz.domain.User;
import com.zxcz.service.UserService;
import com.zxcz.tools.CustomHttpStatus;
import com.zxcz.tools.ResultUtil;

@RestController
@RequestMapping(value = "/user")
public class UserController {
	@Autowired 
	private UserService userService;
	private static final Log logger = LogFactory.getLog(UserController.class);
	
	/**
	 * 分页
	 */
	@RequestMapping("/fingPage")
	public PageResult<User> findPage(int pageNum,int pageSize, User user, String productName) {
		return userService.findPage(pageNum, pageSize, user, productName);
	}
	
	/**
	 * 修改密码
	 */
	@RequestMapping("/modifyPassword")
	public Result<Object> modifyPassword(String password,String passwordConfirm) {
		if(password.equals("") || password==null) {
			return new ResultUtil().generate(CustomHttpStatus.PASSWORD_IS_NULL);
		}
		if(password.length()<6) {
			return new ResultUtil().generate(CustomHttpStatus.PASSWORD_LESS_SIX);
		}
		if(!password.equals(passwordConfirm)) {
			return new ResultUtil().generate(CustomHttpStatus.PASSWORD_NOT_MATCH);
		}
		return userService.modifyPassword(password);
	}
	
	/**
	 * 获取全部用户
	 * @return
	 */
	@RequestMapping("/query")
	public Result<Object> queryAllUser(){
		return userService.queryAllUser();
	}
	
	/**
	 * 获取用户信息
	 * @param session
	 * @return
	 */
	@RequestMapping("/userInfo")
	public Result<Object> getUserInfo() {
		return userService.getUserInfo();
	}
	
	/**
	 * 新建内容商
	 */
	@RequestMapping("/insert")
	public Result<Object> insertUser(User user,String passwordAgain,@RequestParam(defaultValue = "0") int deviceCount) {
		if(user.getAccount().equals("") || user.getAccount()==null) {
			return ResultUtil.generate(CustomHttpStatus.ACCOUNT_IS_NULL);
		}
		if(user.getName().equals("") || user.getName()==null) {
			return ResultUtil.generate(CustomHttpStatus.USERNAME_IS_NULL);
		}
		if(user.getPassword().equals("") || user.getPassword()==null) {
			return new ResultUtil().generate(CustomHttpStatus.PASSWORD_IS_NULL);
		}
		if(user.getPassword().length()<6) {
			return new ResultUtil().generate(CustomHttpStatus.PASSWORD_LESS_SIX);
		}
		if(!user.getPassword().equals(passwordAgain)) {
			return new ResultUtil().generate(CustomHttpStatus.PASSWORD_NOT_MATCH);
		}
		User UserByAccount = userService.findUserByAccount(user.getAccount());
		//不存在相同账号
		if(UserByAccount == null) {
			userService.insertUser(user,deviceCount);
			return new ResultUtil().generate(CustomHttpStatus.SUCCESS);
		}
		return new ResultUtil().generate(CustomHttpStatus.ACCOUNT_IS_EXISTS);
	}
	
	/**
	 * 修改内容商
	 */
	@RequestMapping("/update")
	public Result<Object> updateUser(User user) {
		return userService.updateUser(user);
	}
	
	/**
	 * 删除内容商
	 */
	@RequestMapping("/delete")
	public Result<Object> deleteUser(int id) {
		return userService.deleteUserById(id);
	}
}
